<template>
	<div class="all">
		<div class="left">
			<left></left>
		</div>
		<div class="right">
			<div class="top">
				<top></top>
			</div>
			<div class="content">
				<div class="flex">
					<el-button type="warning" @click="log = true">添加套餐</el-button>
				</div>
				<div class="table">
					<el-table :data="tableData" border style="width: 100%" v-loading="loading">
						<el-table-column label="套餐名称">
							<template slot-scope="scope">
								<span style="margin-left: 10px">{{ scope.row.title }}</span>
							</template>
						</el-table-column>
						<el-table-column label="创建时间">
							<template slot-scope="scope">
								<span style="margin-left: 10px">{{ scope.row.create_time }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作">
							<template slot-scope="scope">
								<el-button type="text" @click="shanchu(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination style="text-align: right; padding: 20px;" background layout="total,prev, pager, next"
						:current-page="page" @current-change="fanye" :page-size="page_size" :total="total">
					</el-pagination>
				</div>
			</div>
		</div>
		<el-dialog title="添加套餐" :visible.sync="log" :close-on-click-modal="false" width="600px">
			<div class="flex">
				<div class="f1">选择套餐:</div>
				<div class="f2">
					<el-select v-model="taocan.package_id" placeholder="请选择套餐">
						<el-option v-for="item in alltaocan" :key="item.id" :label="item.title" :value="item.id">
						</el-option>
					</el-select>
				</div>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="log = false">取 消</el-button>
				<el-button type="primary" @click="tianjia">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import axios from 'axios'
	import top from '@/components/top.vue'
	import left from '@/components/left.vue'
	import bus from '@/assets/bus.js'
	export default {
		name: 'taocan',
		components: {
			left,
			top
		},
		data() {
			return {
				taocan: {
					package_id: ""
				},
				changecon: {},
				log: false,
				page: 1,
				page_size: 20,
				total: 0,
				loading: true,
				tableData: "",
				alltaocan: ""
			}
		},
		watch: {
			// num:function(newValue, oldValue) {
			//   console.log(newValue, oldValue)
			// }
		},
		computed: {
			// zongjia:function(){
			//   return this.num * 100
			// }
		},
		created() {
			this.gettaocan()
			this.getlist()

		},
		methods: {
			shanchu:function(id){
				this.$confirm('是否真的删除此套餐?', '确认提示', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消',
				  type: 'warning'
				}).then(() => {
					this.axios.post(this.url + `/work/packages/deleAgentWithPackageId?agent_id=`+this.$route.query.id+`&package_id=`+ id)
						.then(response => {
							if (response.data.success == true) {
								this.getlist()
								this.$message.success('操作成功');
							} else {
								this.$message.error(response.data.message);
							}
						})
				})
			},
			//获取全部套餐
			gettaocan: function() {
				this.axios.get(this.url + `/work/agent/getPackageList?agent_id=` + this.$route.query.id)
					.then(response => {
						if (response.data.success == true) {
							this.alltaocan = response.data.result
						} else {
							this.$message.error(response.data.message);
						}
					})
			},
			//翻页
			fanye: function(page) {
				this.loading = true
				this.page = page
				this.getlist()
			},
			//获取套餐
			getlist: function() {
				this.axios.get(this.url + `/work/agent/getPackageWithAgentId?agent_id=` + this.$route.query.id)
					.then(response => {
						if (response.data.success == true) {
							this.tableData = response.data.result
							this.loading = false
							
						} else {
							this.$message.error(response.data.message);
						}
					})
			},
			tianjia: function() {
				if (this.taocan.package_id == '') {
					this.$message.error('请选择关联的套餐');
					return false
				}
				this.axios.post(this.url + `/work/agent/addPackageWithAgentId?agent_id=` + this.$route.query.id +
						`&package_id=${this.taocan.package_id}`)
					.then(response => {
						if (response.data.success == true) {
							this.tableData = response.data.result
							this.loading = true
							this.log = false
							this.getlist()
						} else {
							this.$message.error(response.data.message);
						}
					})
			},
			change: function() {
				this.log = true
			}
		},

	}
</script>

<style scoped lang="less">
	.all {
		display: flex;
		height: 100vh;

		.left {
			width: 200px;
			height: 100%;
			background: #414652;
			box-shadow: 0 2px 10px #2D3038;
		}

		.right {
			width: calc(100% - 180px);
			height: 100%;
			overflow: auto;

			.content {
				padding: 20px;

				.table {
					margin-top: 20px;
				}
			}
		}
	}
</style>
